package com.atguigu.flink03;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.connector.file.src.FileSource;
import org.apache.flink.connector.file.src.reader.TextLineInputFormat;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/2/20
 * 该案例演示了从文件中读取数据
 */
public class Flink04_Source_File {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //TODO 2.从指定的文件中读取数据
        //DataStreamSource<String> fileDS = env.readTextFile("D:\\dev\\workspace\\bigdata-0918\\flink-0918\\input\\word.txt");
        FileSource<String> source = FileSource.forRecordStreamFormat(new TextLineInputFormat(), new Path("D:\\dev\\workspace\\bigdata-0918\\flink-0918\\input\\word.txt")).build();

        // 添加数据源(从指定的数据源读取数据 封装为流)
        // Flink1.12前 env.addSource(SourceFunction )
        // Flink1.12后 env.fromSource(Source )

        DataStreamSource<String> fileDS = env.fromSource(source, WatermarkStrategy.noWatermarks(), "file_source");

        fileDS.print();

        env.execute();
    }
}
